package com.yft.util;

import java.security.MessageDigest;

import org.apache.commons.lang.math.NumberUtils;

public class MD5Encrypt {
	public static String EncodeAsNum(String origin) throws Exception {
		MessageDigest md = MessageDigest.getInstance("MD5");
		return byteArrayToString(md.digest(origin.getBytes()), 10);
	}

	public static String EncodeAsHex(String origin) throws Exception {
		if (origin==null) return null;
		
		MessageDigest md = MessageDigest.getInstance("MD5");
		
		return byteArrayToString(md.digest(origin.getBytes()), 16);
	}

	private static String byteArrayToString(byte b[], int type) {
		StringBuffer resultSb = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			if (type == 16)
				resultSb.append(byteToHexString(b[i]));
			if (type == 10)
				resultSb.append(byteToNumString(b[i]));
		}

		return resultSb.toString();
	}

	private static String byteToNumString(byte b) {
		int _b = b;
		if (_b < 0)
			_b += 256;
		return String.valueOf(_b);
	}

	private static String byteToHexString(byte b) {
		int n = b;
		if (n < 0)
			n += 256;
		int d1 = n / 16;
		int d2 = n % 16;
		return (new StringBuilder(String.valueOf(hexDigits[d1]))).append(
				hexDigits[d2]).toString();
	}

	private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

	
	public static void main(String[] args) throws Exception{
		
		System.out.println(MD5Encrypt.EncodeAsHex("IMVHK5"));
		
		System.out.println(NumberUtils.toLong("102323349"));

	}
	
	
	
}
